﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class CustomizedTravel_Entity : EntityBase<CUSTOMIZEDTRAVEL>
    {
        public override RESP_Info Add(CUSTOMIZEDTRAVEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into CUSTOMIZEDTRAVEL(CT_ID,DESTINATION,STARTCITY,TRAVELDAYS,ADULT,CHILD,BUDGET,STARTDATE,THEMES,HOBBYS,ADDITIONAL,USERNAME,USERID,TELEPHONE,EMAIL) values (@CT_ID,@DESTINATION,@STARTCITY,@TRAVELDAYS,@ADULT,@CHILD,@BUDGET,@STARTDATE,@THEMES,@HOBBYS,@ADDITIONAL,@USERNAME,@USERID,@TELEPHONE,@EMAIL)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CT_ID", model.CT_ID));
                    cmd.Parameters.Add(new SqlParameter("DESTINATION", model.DESTINATION));
                    cmd.Parameters.Add(new SqlParameter("STARTCITY", model.STARTCITY));
                    cmd.Parameters.Add(new SqlParameter("TRAVELDAYS", model.TRAVELDAYS));
                    cmd.Parameters.Add(new SqlParameter("ADULT", model.ADULT));
                    cmd.Parameters.Add(new SqlParameter("CHILD", model.CHILD));
                    cmd.Parameters.Add(new SqlParameter("BUDGET", model.BUDGET));
                    cmd.Parameters.Add(new SqlParameter("STARTDATE", model.STARTDATE));
                    cmd.Parameters.Add(new SqlParameter("THEMES", model.THEMES));
                    cmd.Parameters.Add(new SqlParameter("HOBBYS", model.HOBBYS));
                    cmd.Parameters.Add(new SqlParameter("ADDITIONAL", model.ADDITIONAL));
                    cmd.Parameters.Add(new SqlParameter("USERNAME", model.USERNAME));
                    cmd.Parameters.Add(new SqlParameter("USERID", model.USERID));
                    cmd.Parameters.Add(new SqlParameter("TELEPHONE", model.TELEPHONE));
                    cmd.Parameters.Add(new SqlParameter("EMAIL", model.EMAIL));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加定制成功</p></h5>";
                    info.Url = "CustomizedTravelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = false;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加定制失败</p></h5>";
                    info.Url = "CustomizedTravelList.aspx";

                    Tom.Net.Log.LogManager.Error("添加定制失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(CUSTOMIZEDTRAVEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update CUSTOMIZEDTRAVEL set DESTINATION=@DESTINATION,STARTCITY=@STARTCITY,TRAVELDAYS=@TRAVELDAYS,ADULT=@ADULT,CHILD=@CHILD,BUDGET=@BUDGET,STARTDATE=@STARTDATE,THEMES=@THEMES,HOBBYS=@HOBBYS,ADDITIONAL=@ADDITIONAL,USERNAME=@USERNAME,USERID=@USERID,TELEPHONE=@TELEPHONE,EMAIL=@EMAIL  where CT_ID=@CT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CT_ID", model.CT_ID));
                    cmd.Parameters.Add(new SqlParameter("DESTINATION", model.DESTINATION));
                    cmd.Parameters.Add(new SqlParameter("STARTCITY", model.STARTCITY));
                    cmd.Parameters.Add(new SqlParameter("TRAVELDAYS", model.TRAVELDAYS));
                    cmd.Parameters.Add(new SqlParameter("ADULT", model.ADULT));
                    cmd.Parameters.Add(new SqlParameter("CHILD", model.CHILD));
                    cmd.Parameters.Add(new SqlParameter("BUDGET", model.BUDGET));
                    cmd.Parameters.Add(new SqlParameter("STARTDATE", model.STARTDATE));
                    cmd.Parameters.Add(new SqlParameter("THEMES", model.THEMES));
                    cmd.Parameters.Add(new SqlParameter("HOBBYS", model.HOBBYS));
                    cmd.Parameters.Add(new SqlParameter("ADDITIONAL", model.ADDITIONAL));
                    cmd.Parameters.Add(new SqlParameter("USERNAME", model.USERNAME));
                    cmd.Parameters.Add(new SqlParameter("USERID", model.USERID));
                    cmd.Parameters.Add(new SqlParameter("TELEPHONE", model.TELEPHONE));
                    cmd.Parameters.Add(new SqlParameter("EMAIL", model.EMAIL));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑定制成功</p></h5>";
                    info.Url = "CustomizedTravelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑定制失败</p></h5>";
                    info.Url = "CustomizedTravelEdit.aspx";

                    Tom.Net.Log.LogManager.Error("编辑定制失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(CUSTOMIZEDTRAVEL ctravel)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from CUSTOMIZEDTRAVEL where CT_ID=@CT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CT_ID", ctravel.CT_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除定制成功</p></h5>";
                    info.Url = "CustomizedTravelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除定制失败</p></h5>";
                    info.Url = "CustomizedTravelList.aspx";

                    Tom.Net.Log.LogManager.Error("删除定制失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override CUSTOMIZEDTRAVEL GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select  CT_ID,DESTINATION,STARTCITY,TRAVELDAYS,ADULT,CHILD,BUDGET,STARTDATE,THEMES,HOBBYS,ADDITIONAL,USERNAME,USERID,TELEPHONE,EMAIL from CUSTOMIZEDTRAVEL where CT_ID=@CT_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CT_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        CUSTOMIZEDTRAVEL ctravel = new CUSTOMIZEDTRAVEL();
                        if (!reader.IsDBNull(0)) ctravel.CT_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) ctravel.DESTINATION = reader.GetString(1);
                        if (!reader.IsDBNull(2)) ctravel.STARTCITY = reader.GetString(2);
                        if (!reader.IsDBNull(3)) ctravel.TRAVELDAYS = reader.GetInt16(3);
                        if (!reader.IsDBNull(4)) ctravel.ADULT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) ctravel.CHILD = reader.GetInt16(5);
                        if (!reader.IsDBNull(6)) ctravel.BUDGET = reader.GetInt16(6);
                        if (!reader.IsDBNull(7)) ctravel.STARTDATE = reader.GetDateTime(7);
                        if (!reader.IsDBNull(8)) ctravel.THEMES = reader.GetString(8);
                        if (!reader.IsDBNull(9)) ctravel.HOBBYS = reader.GetString(9);
                        if (!reader.IsDBNull(10)) ctravel.ADDITIONAL = reader.GetString(10);
                        if (!reader.IsDBNull(11)) ctravel.USERNAME = reader.GetString(11);
                        if (!reader.IsDBNull(12)) ctravel.USERID = reader.GetString(12);
                        if (!reader.IsDBNull(13)) ctravel.TELEPHONE = reader.GetString(13);
                        if (!reader.IsDBNull(14)) ctravel.EMAIL = reader.GetString(14);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return ctravel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override CUSTOMIZEDTRAVEL GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        CUSTOMIZEDTRAVEL ctravel = new CUSTOMIZEDTRAVEL();
                        if (!reader.IsDBNull(0)) ctravel.CT_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) ctravel.DESTINATION = reader.GetString(1);
                        if (!reader.IsDBNull(2)) ctravel.STARTCITY = reader.GetString(2);
                        if (!reader.IsDBNull(3)) ctravel.TRAVELDAYS = reader.GetInt16(3);
                        if (!reader.IsDBNull(4)) ctravel.ADULT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) ctravel.CHILD = reader.GetInt16(5);
                        if (!reader.IsDBNull(6)) ctravel.BUDGET = reader.GetInt16(6);
                        if (!reader.IsDBNull(7)) ctravel.STARTDATE = reader.GetDateTime(7);
                        if (!reader.IsDBNull(8)) ctravel.THEMES = reader.GetString(8);
                        if (!reader.IsDBNull(9)) ctravel.HOBBYS = reader.GetString(9);
                        if (!reader.IsDBNull(10)) ctravel.ADDITIONAL = reader.GetString(10);
                        if (!reader.IsDBNull(11)) ctravel.USERNAME = reader.GetString(11);
                        if (!reader.IsDBNull(12)) ctravel.USERID = reader.GetString(12);
                        if (!reader.IsDBNull(13)) ctravel.TELEPHONE = reader.GetString(13);
                        if (!reader.IsDBNull(14)) ctravel.EMAIL = reader.GetString(14);


                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return ctravel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<CUSTOMIZEDTRAVEL> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<CUSTOMIZEDTRAVEL> ctravels = new List<CUSTOMIZEDTRAVEL>();
                    while (reader.Read())
                    {
                        CUSTOMIZEDTRAVEL ctravel = new CUSTOMIZEDTRAVEL();
                        if (!reader.IsDBNull(0)) ctravel.CT_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) ctravel.DESTINATION = reader.GetString(1);
                        if (!reader.IsDBNull(2)) ctravel.STARTCITY = reader.GetString(2);
                        if (!reader.IsDBNull(3)) ctravel.TRAVELDAYS = reader.GetInt16(3);
                        if (!reader.IsDBNull(4)) ctravel.ADULT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) ctravel.CHILD = reader.GetInt16(5);
                        if (!reader.IsDBNull(6)) ctravel.BUDGET = reader.GetInt16(6);
                        if (!reader.IsDBNull(7)) ctravel.STARTDATE = reader.GetDateTime(7);
                        if (!reader.IsDBNull(8)) ctravel.THEMES = reader.GetString(8);
                        if (!reader.IsDBNull(9)) ctravel.HOBBYS = reader.GetString(9);
                        if (!reader.IsDBNull(10)) ctravel.ADDITIONAL = reader.GetString(10);
                        if (!reader.IsDBNull(11)) ctravel.USERNAME = reader.GetString(11);
                        if (!reader.IsDBNull(12)) ctravel.USERID = reader.GetString(12);
                        if (!reader.IsDBNull(13)) ctravel.TELEPHONE = reader.GetString(13);
                        if (!reader.IsDBNull(14)) ctravel.EMAIL = reader.GetString(14);

                        ctravels.Add(ctravel);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return ctravels;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}